package com.shuyun.ccms.encrypt.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shuyun.ccms.encrypt.util.ConnectionUtil;

public class DataCheck {
	private static Logger logger = LoggerFactory.getLogger(DataCheck.class);
	public  static boolean checkTableExist(Connection conn, String tableName)throws Exception{
		boolean result = false;
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		try {
			sql.append("show tables like '").append(tableName).append("'");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while(rs.next()){
				if(tableName.equals(rs.getString(1))){
					result=true;
					logger.info(tableName + "存在，该平台对应客户信息需加密");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("检查plt表是否存在报错. " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
		}
		return result;
	}

	public  static boolean checkRelationTableData(Connection conn, String tableName)throws Exception{
		boolean result = false;
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		int count = 0;
		try {
			sql.append("select count(1) from  ").append(tableName);
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while(rs.next()){
				count = rs.getInt(1);
			}
			if(count> 0){
				result=true;
				logger.info(tableName + "中已有数据，需部分新增。");
			}else{
				result = false;
				logger.info(tableName + "中无数据，需全量导入。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("检查表: " + tableName +"是否有数据异常: " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
		}
		return result;
	}
}
